package com.itheima.test3;

public class Test3Polymorphic {
    public static void main(String[] args) {
        useDog(new Dog());
        useCat(new Cat());
        useAnimal(new Dog());
        useAnimal(new Cat());
    }

    public static void useDog(Dog dog) {
        dog.eat();
    }

    public static void useCat(Cat cat) {
        cat.eat();
    }

    public static void useAnimal(Animal animal) {
        animal.eat();
//        animal.watchHome();
        if (animal instanceof Dog) {
            Dog dog = (Dog) animal; // 类型转换错误
            dog.watchHome();
        }
    }
}

abstract class Animal {
    public abstract void eat();
}

class Dog extends Animal {

    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }

    public void watchHome() {
        System.out.println("看家");
    }
}

class Cat extends Animal {

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}